<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属组织ID" prop="organizationId">
        <el-input
          v-model="queryParams.organizationId"
          placeholder="请输入所属组织ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织类型" prop="organizationType">
        <el-select v-model="queryParams.organizationType" placeholder="请选择组织类型" clearable>
          <el-option
            v-for="dict in dict.type.app_organization_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择活动状态" clearable>
          <el-option
            v-for="dict in dict.type.app_activitiesstatis"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="固定活动类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择固定活动类别" clearable>
          <el-option
            v-for="dict in dict.type.app_activitiestype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入活动地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参与人数限制" prop="participantLimit">
        <el-input
          v-model="queryParams.participantLimit"
          placeholder="请输入参与人数限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名开始时间" prop="signupStart">
        <el-date-picker clearable
          v-model="queryParams.signupStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报名结束时间" prop="signupEnd">
        <el-date-picker clearable
          v-model="queryParams.signupEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用类型" prop="feeType">
        <el-select v-model="queryParams.feeType" placeholder="请选择费用类型" clearable>
          <el-option
            v-for="dict in dict.type.app_activitiesfeiyongtype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动费用" prop="fee">
        <el-input
          v-model="queryParams.fee"
          placeholder="请输入活动费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人ID" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:activities:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:activities:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:activities:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:activities:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activitiesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动唯一标识" align="center" prop="activityId" />
      <el-table-column label="所属组织ID" align="center" prop="organizationId" />
      <el-table-column label="组织类型" align="center" prop="organizationType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_organization_type" :value="scope.row.organizationType"/>
        </template>
      </el-table-column>
      <el-table-column label="活动标题" align="center" prop="title" />
      <el-table-column label="活动主图" align="center" prop="mainImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.mainImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="轮播图" align="center" prop="bannerImages" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.bannerImages" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_activitiesstatis" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="固定活动类别" align="center" prop="category">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_activitiestype" :value="scope.row.category"/>
        </template>
      </el-table-column>
      <el-table-column label="自选标签" align="center" prop="customTags">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_zxactivitiestype" :value="scope.row.customTags ? scope.row.customTags.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="活动详情" align="center" prop="description" />
      <el-table-column label="活动开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="location" />
      <el-table-column label="参与人数限制" align="center" prop="participantLimit" />
      <el-table-column label="报名开始时间" align="center" prop="signupStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signupStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="signupEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signupEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用类型" align="center" prop="feeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_activitiesfeiyongtype" :value="scope.row.feeType"/>
        </template>
      </el-table-column>
      <el-table-column label="活动费用" align="center" prop="fee" />
      <el-table-column label="费用说明" align="center" prop="feeDesc" />
      <el-table-column label="创建人ID" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:activities:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:activities:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改校友会活动（无物理外键，通过业务逻辑维护关联）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属组织ID" prop="organizationId">
          <el-input v-model="form.organizationId" placeholder="请输入所属组织ID" />
        </el-form-item>
        <el-form-item label="组织类型" prop="organizationType">
          <el-select v-model="form.organizationType" placeholder="请选择组织类型">
            <el-option
              v-for="dict in dict.type.app_organization_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动主图" prop="mainImage">
          <image-upload v-model="form.mainImage"/>
        </el-form-item>
        <el-form-item label="轮播图" prop="bannerImages">
          <image-upload v-model="form.bannerImages"/>
        </el-form-item>
        <el-form-item label="活动状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择活动状态">
            <el-option
              v-for="dict in dict.type.app_activitiesstatis"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="固定活动类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择固定活动类别">
            <el-option
              v-for="dict in dict.type.app_activitiestype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自选标签" prop="customTags">
          <el-checkbox-group v-model="form.customTags">
            <el-checkbox
              v-for="dict in dict.type.app_zxactivitiestype"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="活动详情" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入活动地点" />
        </el-form-item>
        <el-form-item label="参与人数限制" prop="participantLimit">
          <el-input v-model="form.participantLimit" placeholder="请输入参与人数限制" />
        </el-form-item>
        <el-form-item label="报名开始时间" prop="signupStart">
          <el-date-picker clearable
            v-model="form.signupStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名结束时间" prop="signupEnd">
          <el-date-picker clearable
            v-model="form.signupEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用类型" prop="feeType">
          <el-select v-model="form.feeType" placeholder="请选择费用类型">
            <el-option
              v-for="dict in dict.type.app_activitiesfeiyongtype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动费用" prop="fee">
          <el-input v-model="form.fee" placeholder="请输入活动费用" />
        </el-form-item>
        <el-form-item label="费用说明" prop="feeDesc">
          <el-input v-model="form.feeDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivities, getActivities, delActivities, addActivities, updateActivities } from "@/api/app/activities"

export default {
  name: "Activities",
  dicts: ['app_zxactivitiestype', 'app_organization_type', 'app_activitiesstatis', 'app_activitiesfeiyongtype', 'app_activitiestype'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 校友会活动（无物理外键，通过业务逻辑维护关联）表格数据
      activitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizationId: null,
        organizationType: null,
        title: null,
        mainImage: null,
        bannerImages: null,
        status: null,
        category: null,
        customTags: null,
        description: null,
        startTime: null,
        endTime: null,
        location: null,
        participantLimit: null,
        signupStart: null,
        signupEnd: null,
        feeType: null,
        fee: null,
        feeDesc: null,
        createdBy: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        organizationId: [
          { required: true, message: "所属组织ID不能为空", trigger: "blur" }
        ],
        organizationType: [
          { required: true, message: "组织类型不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "活动标题不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "活动状态不能为空", trigger: "change" }
        ],
        startTime: [
          { required: true, message: "活动开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "活动结束时间不能为空", trigger: "blur" }
        ],
        feeType: [
          { required: true, message: "费用类型不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询校友会活动（无物理外键，通过业务逻辑维护关联）列表 */
    getList() {
      this.loading = true
      listActivities(this.queryParams).then(response => {
        this.activitiesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        organizationId: null,
        organizationType: null,
        title: null,
        mainImage: null,
        bannerImages: null,
        status: null,
        category: null,
        customTags: [],
        description: null,
        startTime: null,
        endTime: null,
        location: null,
        participantLimit: null,
        signupStart: null,
        signupEnd: null,
        feeType: null,
        fee: null,
        feeDesc: null,
        createdBy: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加校友会活动（无物理外键，通过业务逻辑维护关联）"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const activityId = row.activityId || this.ids
      getActivities(activityId).then(response => {
        this.form = response.data
        this.form.customTags = this.form.customTags.split(",")
        this.open = true
        this.title = "修改校友会活动（无物理外键，通过业务逻辑维护关联）"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.customTags = this.form.customTags.join(",")
          if (this.form.activityId != null) {
            updateActivities(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addActivities(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids
      this.$modal.confirm('是否确认删除校友会活动（无物理外键，通过业务逻辑维护关联）编号为"' + activityIds + '"的数据项？').then(function() {
        return delActivities(activityIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/activities/export', {
        ...this.queryParams
      }, `activities_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
